package club.ljwang.controller;

import club.ljwang.entity.Result;
import club.ljwang.pojo.HotSetmeal;
import club.ljwang.pojo.ReportData;
import club.ljwang.service.ReportService;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * @Auther: wangl
 * @Date: 2020/3/7 21:43
 * @Description:
 */

@RestController
@RequestMapping("report")
public class ReportController {


    @Reference
    private ReportService reportService;

    @RequestMapping("getMemberReport")
    public Result getMemberReport() {

        DateTime now = new DateTime();
        DateTime start = DateUtil.offsetMonth(now, -12);
        ArrayList<String> months = new ArrayList<>();

        for (int i = 0; i <= 12; i++) {
            DateTime month = DateUtil.offsetMonth(start, i);
            months.add(month.toString("yyyy-MM"));
        }
        ArrayList<Integer> memberCount = reportService.getMemberReport(months);
        HashMap map = new HashMap();
        map.put("months", months);
        map.put("memberCount", memberCount);
        return new Result(true, "success", map);

    }

    @RequestMapping("getMemberReportByDayDiff")
    public Result getMemberReportByDayDiff(@RequestBody Map map) {

        String dateStr1 = (String) map.get("start");
        String dateStr2 = (String) map.get("end");

        Date date1 = DateUtil.parse(dateStr1);

        Date date2 = DateUtil.parse(dateStr2);

        long betweenDay = DateUtil.between(date1, date2, DateUnit.DAY);
        System.out.println("betweenDay = " + betweenDay);
        ArrayList<String> days = new ArrayList<>();
        for (int i = 0; i <= betweenDay; i++) {
            DateTime day = DateUtil.offsetDay(date1,i);
            days.add(day.toString("yyyyMMdd"));
        }

        ArrayList<Integer> memberCount = reportService.getMemberReportByDays(days);

        map.put("days", days);
        map.put("memberCount", memberCount);
        return new Result(true, "success", map);

    }

    @RequestMapping("getSetmealReport")
    public Result getSetmealReport() {

        List<Map> mapList = reportService.getSetmealReport();
        return new Result(true, "success", mapList);
    }

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        ReportData reportData = reportService.getBusinessReportData();


        return new Result(true, "success", reportData);
    }

    @RequestMapping("/getMemberCombo")
    public Result getMemberCombo(){

        Map map = reportService.getMemberCombo();


        return new Result(true, "success", map);
    }

    @RequestMapping("exportBusinessReport1")
    public void exportBusinessReport(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ReportData report = reportService.getBusinessReportData();

        String path = request.getSession().getServletContext().getRealPath("template");
        XSSFWorkbook workbook = new XSSFWorkbook(path + "/report_template.xlsx");
        XSSFSheet sheet0 = workbook.getSheetAt(0);

        sheet0.getRow(2).getCell(5).setCellValue(report.getReportDate());

        sheet0.getRow(4).getCell(5).setCellValue(report.getTodayNewMember());
        sheet0.getRow(4).getCell(7).setCellValue(report.getTotalMember());
        sheet0.getRow(5).getCell(5).setCellValue(report.getThisWeekNewMember());
        sheet0.getRow(5).getCell(7).setCellValue(report.getThisMonthNewMember());


        sheet0.getRow(7).getCell(5).setCellValue(report.getTodayOrderNumber());
        sheet0.getRow(7).getCell(7).setCellValue(report.getTodayVisitsNumber());
        sheet0.getRow(8).getCell(5).setCellValue(report.getThisWeekOrderNumber());
        sheet0.getRow(8).getCell(7).setCellValue(report.getThisWeekVisitsNumber());
        sheet0.getRow(9).getCell(5).setCellValue(report.getThisMonthOrderNumber());
        sheet0.getRow(9).getCell(7).setCellValue(report.getThisMonthVisitsNumber());

        int i = 12;
        for (HotSetmeal hotSetmeal : report.getHotSetmeal()) {
            sheet0.getRow(i).getCell(4).setCellValue(hotSetmeal.getName());
            sheet0.getRow(i).getCell(5).setCellValue(hotSetmeal.getSetmealCount());
            sheet0.getRow(i).getCell(6).setCellValue(hotSetmeal.getProportion().doubleValue());
            i ++;
        }
        //利用response把文件写回到浏览器
        //告诉浏览器我现在返回的是一个excel
        response.setHeader("Content-Type","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition","attachment;filename="+ java.net.URLEncoder.encode("运营数据报表", "UTF-8")+".xlsx");
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
        out.close();
        workbook.close();

    }



}
